package cn.ac.aircas.web.controller.system;

import cn.ac.aircas.common.annotation.Anonymous;
import cn.ac.aircas.common.annotation.Cache;
import cn.ac.aircas.common.core.controller.BaseController;
import cn.ac.aircas.common.core.domain.R;
import cn.ac.aircas.system.domain.vo.RoadConditionPointVO;
import cn.ac.aircas.system.service.IRoadPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/roadConditionPoint")
@Api(tags = "路况点请求处理")
public class RoadPointConditionController extends BaseController {

    @Autowired
    IRoadPointService roadPointService;

    /**
     * 查询地下检测概况
     */
    @ApiOperation("查询地下检测概况")
    @PostMapping("/getUndergroundNum/{cityName}")
//    @Anonymous
    public R getUndergroundNum(@PathVariable(required = false) String cityName) {
        Map<String, Integer> undergroundNum = roadPointService.getUndergroundNum(cityName);
        if (undergroundNum == null) {
            undergroundNum = new HashMap<>();
            undergroundNum.put("hollowNum", 0);
            undergroundNum.put("pipNum", 0);
            undergroundNum.put("roadNum", 0);
        }
        return R.ok(undergroundNum);
    }

    /**
     * 查询地下空洞实况
     */
    @ApiOperation("查询地下状况详细信息")
    @PostMapping("/getUnderHollowStat")
    @Cacheable(value = "underground", key = "'getUndergroundStat_'+#param")
//    @Anonymous
    public R<List<RoadConditionPointVO>> getUndergroundStat(@RequestBody(required = false) Map<String, Object> param) {
        List<RoadConditionPointVO> stat = roadPointService.getUnderHollowStat(param);
        return R.ok(stat);
    }

    @ApiOperation("查询地下状况详细信息2")
    @PostMapping("/getUnderHollowStat2")
    @Cacheable(value = "underground", key = "'getUndergroundStat2_'+#param")
//    @Anonymous
    public R<Map<String, List<RoadConditionPointVO>>> getUndergroundStat2(@RequestBody(required = false) Map<String, Object> param) {
        List<RoadConditionPointVO> stat = roadPointService.getUnderHollowStat(param);
        if (param == null || param.get("exceptionType") == null) {
            List<RoadConditionPointVO> pipList = new ArrayList<>();
            List<RoadConditionPointVO> hollowList = new ArrayList<>();
            List<RoadConditionPointVO> roadList = new ArrayList<>();
            Map<String, List<RoadConditionPointVO>> res = new HashMap<>();
            stat.parallelStream().forEach(item -> {
                if (item.getExceptionType() == 9) {
                    roadList.add(item);
                } else if (item.getExceptionType() == 7) {
                    hollowList.add(item);
                } else if (item.getExceptionType() == 8) {
                    pipList.add(item);
                }
            });
            res.put("pipList", pipList);
            res.put("hollowList", hollowList);
            res.put("roadList", roadList);
            return R.ok(res);
        } else {
            Map<String, List<RoadConditionPointVO>> res = new HashMap<>();
            res.put("list", stat);
            return R.ok(res);
        }
    }

    @ApiOperation("根据行政区统计空洞数量")
    @PostMapping("getHollowsInRegion/{cityName}")
//    @Anonymous
    public R getHollowsInRegion(@PathVariable(required = false) String cityName) {
        List<Map> res = roadPointService.getHollowsInRegion(cityName);
        return R.ok(res);
    }
}
